package main.News;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import main.PMF;
import main.UserView;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;

import DBClasses.EGSImage;
import DBClasses.NewsLetter;

public class AddNews extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		String header = null;
		String text = null;
		String id = "";
		PersistenceManager manager = PMF.get().getPersistenceManager();
		try
		{
			ServletFileUpload upload = new ServletFileUpload();
			upload.setSizeMax(1000000);
			FileItemIterator iterator = upload.getItemIterator(req);
			while (iterator.hasNext())
			{
				FileItemStream item = iterator.next();
				InputStream stream = item.openStream();
				byte[] buffer = new byte[1000000];
				int len;
				len = stream.read(buffer);
				if (item.getFieldName().equals("header_txt"))
				{
					if (len > 0)
						header = new String(buffer, 0, len);
					else
						header = "";
					continue;
				}
				if (item.getFieldName().equals("text_txt"))
				{
					if (len > 0)
						text = new String(buffer, 0, len);
					else
						text = "";
					continue;
				}
				if (!item.isFormField())
				{
					if (len > 0)
					{
						byte[] img_buf = new byte[1000000];
						for (int i = 0; i < len; i++)
							img_buf[i] = buffer[i];
						int cur_ln = stream.read(buffer);						
						while (cur_ln > 0)
						{							
							for (int i = len; i < len+cur_ln; i++)
								img_buf[i] = buffer[i-len];		
							len += cur_ln;
							cur_ln = stream.read(buffer);
						}
						EGSImage newe_img = new EGSImage(img_buf);
						manager.makePersistent(newe_img);
						id = newe_img.getId().toString();
					}
				}
			}
		} catch (Exception ex)
		{
			resp.getWriter().write("img");
			return;
		}
		if (header == null || header.equals(""))
		{
			resp.getWriter().write("header");
			return;
		} else
		{
			if (text == null || text.equals(""))
			{
				resp.getWriter().write("text");
				return;
			} else
			{
				NewsLetter nl = new NewsLetter(header, text, id,
						((UserView) req.getSession().getAttribute("user"))
								.getUser().getLogin());
				manager.makePersistent(nl);
				manager.close();
				resp.getWriter().write("success");
			}
		}
	}
}
